<?php
declare(strict_types=1);

namespace App\Common\Modular\Administer\Event;

use App\Common\Modular\Administer\Event\AdministerUser\AdministerUserLogin;
use App\Common\Modular\Administer\Serve\LoginLogServe;
use App\Common\Modular\Administer\Serve\UserServe;
use Core\HyperfUnit\Client;
use Core\HyperfUnit\Ip;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * 后台管理员账号监听器
 */
#[Listener]
class AdministerUserListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            AdministerUserLogin::class
        ];
    }

    public function process(object $event): void
    {
        /**
         * 后台管理员登陆事件
         */
        if ($event instanceof AdministerUserLogin) {
            //清空负面状态
            container(UserServe::class)->search(['id' => $event->user_id])->update([
                'last_login' => time(),
                'locking' => 0,
                'error' => 0
            ]);
            //写入登录日志
            container(LoginLogServe::class)->created([
                'user_id' => $event->user_id,
                'type' => $event->LogType,
                'os' => Client::os(),
                'browser' => Client::browser(),
                'ip' => Ip::getIp()
            ]);
        }
    }
}